var gameInfo = require("gameStrategy");
var GameState = require("gameState");

/**
 * 玩家
 * 支持玩家的基本操作
 */

cc.Class({
  extends: cc.Component,

  properties: {

    halfScreenWidth: 960,
    halfScreenHeight: 640,
    halfPlayerWidth: 100,
    state: GameState.STOP,
    speed: 0,
    movingflag: false,

    smokeNode: {
      default: null,
      type: cc.Node
    },

    spinePlayerNode: {
      default: null,
      type: cc.Node
    },

    diePlayerNode: {
      default: null,
      type: cc.Node
    },

    spineSuperPlayerNode: {
      default: null,
      type: cc.Node
    },

    bianshenNode: {
      default: null,
      type: cc.Node
    },

    superEffectNode: {
      default: null,
      type: cc.Node
    },

    paoBuSource: {
        type: cc.AudioSource,
        default: null
    },

    bianShenClip: {
        url: cc.AudioClip,
        default: null
    },

    guideUiNode: {
      default: null,
      type: cc.Node
    },

  },

  // use this for initialization
  onLoad: function () {

    this.id = 0;

    var screen = cc.view.getVisibleSize();
    this.halfScreenWidth = screen.width / 2;
    this.halfScreenHeight = screen.height / 2;
    this.halfWayWidth = gameInfo.gameRunInfo.wayWidth * gameInfo.gameRunInfo.wayNumber / 2;

    this.smokeAnimation = this.smokeNode.getComponent(cc.Animation);
    this.bianshenAnimation = this.bianshenNode.getComponent(cc.Animation);
    this.superEffectAnimation = this.superEffectNode.getComponent(cc.Animation);
    this.spinePlayerAnimation = this.spinePlayerNode.getComponent('sp.Skeleton');
    this.spineSuperPlayerAnimation = this.spineSuperPlayerNode.getComponent('sp.Skeleton');
    this.currentPlayerNode = this.spinePlayerNode;
    this.currentPlayerAnimation = this.spinePlayerAnimation;

    this.diePlayerNode.active = false;
    this.currentPlayerAnimation.setToSetupPose();
    this.currentPlayerAnimation.clearTrack(0);

    var canvas = cc.find('Canvas');
    canvas.on(cc.Node.EventType.TOUCH_START, this.onTouchBegan, this);
    canvas.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
    canvas.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
    canvas.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
  },

  onTouchBegan: function (event) {
    if (this.state != GameState.RUNNING) {
      cc.log("----> onTouchBegan return null.  game state = " + this.state);
      return;
    }

    var scene = cc.director.getScene();
    var touchLoc = event.touch.getLocation();
    cc.log("----> onTouchBegan playerManager pos: " + this.node.x + ", " + this.node.y + "  : " + touchLoc.x + ", " + touchLoc.y);
    //控制手指只能在人物图片上开始触摸，才能触发移动
    // if (this.isValid(touchLoc.x) && Math.abs(touchLoc.x - this.halfScreenWidth - this.node.x) < this.halfPlayerWidth) {
    //   cc.log("----> onTouchBegan playerManager start! ");
    //   this.movingflag = true;
    //   this.startShow();
    // }
    this.movingflag = true;
    this.startShow();
    this.guideUiNode.active = false;
  },

  onTouchMove: function (event) {
    if (this.state != GameState.RUNNING) {
      return;
    }

    if (this.movingflag) {
      var scene = cc.director.getScene();
      var touchLoc = event.touch.getLocation();
      // cc.log("---->onTouchMove playerManager pos: " + this.node.x + ", " + this.node.y + "  : " + touchLoc.x + ", " + touchLoc.y);
      if (this.isValid(touchLoc.x)) {
        this.node.x = touchLoc.x - this.halfScreenWidth;
        this.node.y = touchLoc.y - 600;//this.halfScreenWidth;
      }
    }
  },

  isValid: function (posX) {
    //并且触摸位置在道路正中间有效区域
    if (Math.abs(posX - this.halfScreenWidth) < this.halfWayWidth) {
      return true;
    }
    return false;
  },

  onTouchEnd: function (event) {
    this.movingflag = false;
    // this.pauseShow();
    cc.log("playerManager onTouchEnd end! ");
  },

  onTouchCancel: function (event) {
    this.movingflag = false;
    // this.pauseShow();
    cc.log("playerManager onTouchBegan cancel! ");
  },

  startShow()
  {
    this.diePlayerNode.active = false;

    this.smokeNode.active = true;
    this.smokeAnimation.play();

    this.currentPlayerNode.active = true;
    this.currentPlayerAnimation.setAnimation(0, 'run', true);
  },

  stopShow()
  {
    this.diePlayerNode.active = true;
    this.bianshenNode.active = true;
    this.bianshenAnimation.play();

    this.smokeNode.active = false;
    this.smokeAnimation.stop();

    this.currentPlayerNode.active = false;
    this.currentPlayerAnimation.setToSetupPose();
    this.currentPlayerAnimation.clearTrack(0);
  },

  // called every frame
  update: function (dt) {
  },

  onCollisionEnter: function (other, self) {
    //碰撞展现效果
    // cc.log(other);
    // cc.log(self);
  },

  // 开始
  running: function (speed) {
    this.state = GameState.RUNNING;
    this.startShow();
    this.paoBuSource.play();
  },

  // 暂停
  pause: function () {
    this.state = GameState.PAUSE;
    this.startShow();
    this.paoBuSource.pause();
  },

  // 恢复
  continue: function () {
    this.state = GameState.RUNNING;
    this.startShow();
    this.paoBuSource.play();
  },

  // 停止
  stop: function () {
    this.state = GameState.STOP;
    this.stopShow();
    this.paoBuSource.stop();
  },

  // 设置阶段
  setStageInfo: function (stageInfo) {
    this.stageInfo = stageInfo;
  },

  // 设置速度
  setSpeed: function (value) {
  },

  changePlayerStateNormal: function (){
    this.bianshenNode.active = false;
    this.bianshenAnimation.stop();
    this.superEffectNode.active = false;
    this.superEffectAnimation.stop();

    //停止变身时的声音
    cc.audioEngine.stop(this.id);

    //恢复原形
    this.spineSuperPlayerNode.active = false;
    this.spinePlayerNode.active = true;
    this.currentPlayerNode = this.spinePlayerNode;
    this.currentPlayerAnimation = this.spinePlayerAnimation;
  },

  changePlayerStateSuper: function (){
    this.bianshenNode.active = true;
    this.bianshenAnimation.play();
    this.superEffectNode.active = true;
    this.superEffectAnimation.play();

    //变身时的声音
    this.id = cc.audioEngine.play(this.bianShenClip, false, 1);

    //变身
    this.spinePlayerNode.active = false;
    this.spineSuperPlayerNode.active = true;
    this.currentPlayerNode = this.spineSuperPlayerNode;
    this.currentPlayerAnimation = this.spineSuperPlayerAnimation;
  },

  //游戏开始前的准备工作
  prepareRunning: function(){
    this.guideUiNode.active = true;
  },

  //玩家死亡，播放死亡动画
  playerDie: function() {
    this.diePlayerNode.active = true;
    this.stopShow();
  }


});

